package com.winit.openapi.interceptor.validator.impl;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.winit.common.spi.context.CommandContext;
import com.winit.erp.spi.bpartner.command.BpartnerCommand;
import com.winit.erp.spi.bpartner.vo.Bpartner;
import com.winit.erp.spi.user.vo.User;
import com.winit.openapi.apiadapter.adapter.UmsUserServiceAdapter;
import com.winit.openapi.constants.ApiConstant;
import com.winit.openapi.constants.ErrorCode;
import com.winit.openapi.exception.ApiException;
import com.winit.openapi.interceptor.validator.FxAPIRequestValidator;
import com.winit.openapi.model.RequestMsg;
import com.winit.openapi.util.RequestUtil;
import com.winit.openapi.util.ValidateUtil;

@Service("fxRequestHandler")
public class FxRequestHandler extends FxAPIRequestValidator<HttpServletRequest> {

    private static final Logger   logger = LoggerFactory.getLogger(FxRequestHandler.class);
    @Resource
    private UmsUserServiceAdapter umsUserServiceAdapter;

    public void checkFxUser(HttpServletRequest request) throws ApiException {
        RequestMsg requestMsg = RequestUtil.getRequestMsg(request);
        if (StringUtils.isBlank(requestMsg.getAuz_app_key())) {
            return;
        }
        User user = umsUserServiceAdapter.queryFxUserByName(requestMsg.getAuz_app_key());
        if (null == user) {
            logger.warn("UMS查询分销用户信息失败，User:" + requestMsg.getAuz_app_key());
            throw new ApiException(ErrorCode.USER_NOT_FOUND);
        }
        // 校验分销商公司信息
        BpartnerCommand command = new BpartnerCommand();
        command.setcBpartnerId(user.getBpartnerId());
        Bpartner auzBpartner = umsUserServiceAdapter.queryFxCompany(user);
        if (auzBpartner == null) {
            throw new ApiException(ErrorCode.BPARTNER_NOT_FOUND_ERROR);
        }

        CommandContext ctx = CommandContext.getContext();
        // 分销商用户和签名校验
        String fxBpartnerCode = auzBpartner.getValue();
        // 分销商客户编码
        ctx.set(ApiConstant.API_DIS_USER, user);
        ctx.set(ApiConstant.API_DIS_BPARTNER_CODE, fxBpartnerCode);
    }

    public void checkFxSign(HttpServletRequest request) throws ApiException {
        RequestMsg requestMsg = RequestUtil.getRequestMsg(request);
        if (requestMsg == null) {
            return;
        }
        String sign = requestMsg.getAuz_sign();
        CommandContext ctx = CommandContext.getContext();
        User user = ctx.get(ApiConstant.API_DIS_USER);
        checkSign(request, user.getToken(), sign);

    }

    @Override
    public void doAuth(HttpServletRequest request) {
        checkFxUser(request);
        checkFxSign(request);

    }

    @Override
    public void doCheckData(HttpServletRequest request) throws ApiException {
        JSONObject json = super.checkBaseData(request);
        ValidateUtil.validNotNull(json, "auz_app_key");
        ValidateUtil.validMaxLength(json, "auz_app_key", 50);
        // sign
        ValidateUtil.validNotNull(json, "auz_sign");
        ValidateUtil.validMaxLength(json, "auz_sign", 32);
    }

}
